//
//  HerosNextChuzhuangTableViewController.m
//  LOLBOX
//
//  Created by lanou3g on 15/12/17.
//  Copyright © 2015年 张明. All rights reserved.
//

#import "HerosNextChuzhuangTableViewController.h"
#import "HerosPifuModelHelper.h"
#import "TableViewCell.h"
#import "HeroHelper.h"
#import "HerosChuZhuangModel.h"
#import "HeroNextCzNextTableViewController.h"
#import "HerosChuZhuangModel.h"
#import "KVNProgress.h"
#import "KVNProgressConfiguration.h"
static CGFloat kImageHeight = 220.f;
@interface HerosNextChuzhuangTableViewController ()
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *nameLable;
@property(nonatomic,strong)UILabel *typeLable;
@property(nonatomic,strong)UILabel *priceLable;
@property(nonatomic,strong)NSMutableArray *allModelArray;
//小菊花
@property(nonatomic,strong)KVNProgressConfiguration *kVn;
@end

@implementation HerosNextChuzhuangTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.allModelArray = [NSMutableArray array];
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    self.kVn.fullScreen = YES;
    [KVNProgress showWithStatus:@"玩命加载中..."];
    self.tableView.frame = CGRectMake(0, 40, self.tableView.frame.size.width, self.tableView.frame.size.height *0.85);
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:@"twoCell"];
    
    self.tableView.contentInset = UIEdgeInsetsMake(kImageHeight, 0, 0, 0);
    self.imageView = [[UIImageView alloc] init];
    self.imageView.frame = CGRectMake(0, -kImageHeight , self.tableView.frame.size.width, kImageHeight);
    [self createImageViewChild];
    [self.view addSubview:self.imageView];
    //添加皮肤置顶部
    HerosPifuModelHelper *helper = [HerosPifuModelHelper defaultHerosPifuHelper];
    if (helper.nameArray.count >=2) {
        self.nameLable.text = helper.nameArray[1];
    }else{
        self.nameLable.text = helper.nameArray[0];
    }
    if (helper.imageArray.count >=2) {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:helper.imageArray[1]]];
    }else{
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:helper.imageArray[0]]];
    }
    NSArray *array1 = [helper.price componentsSeparatedByString:@","];
    self.priceLable.text = [NSString stringWithFormat:@"金币:%@ 点卷:%@",array1[0],array1[1]];
    self.typeLable.text = helper.type;
//数据请求
    [[HeroHelper shareHeroWithHelpData]getHeroDataWithUrl2:[NSString stringWithFormat:@"http://db.duowan.com/lolcz/img/ku11/api/lolcz.php?v=108&OSType=iOS8.3&championName=%@&limit=7",self.aString] Key:nil successData:^(id responseObject) {
        NSArray *array = responseObject;
        // - -- - -数据解析英雄出装- - -
        for (NSDictionary *dic in array) {
            HerosChuZhuangModel *model = [[HerosChuZhuangModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.allModelArray addObject:model];
            NSLog(@"allArray- - --%ld",self.allModelArray.count);
        }
        [self.tableView reloadData];
        [KVNProgress dismiss];
    } fail:^(NSError *err) {
         NSLog(@"出装数据请求失败");
    }];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//隐藏cell分割线
    
}

#pragma mark - 添加控件
-(void)createImageViewChild{
    self.nameLable = [[UILabel alloc] init];
    self.nameLable.frame = CGRectMake(10, self.imageView.frame.size.height -80, self.view.frame.size.width-20, 20);
    // self.nameLable.backgroundColor = [UIColor redColor];
    self.nameLable.font = [UIFont systemFontOfSize:14];
    self.nameLable.textColor = [UIColor whiteColor];
    [self.imageView addSubview:self.nameLable];
    
    self.typeLable = [[UILabel alloc] init];
    self.typeLable.frame = CGRectMake(10, self.imageView.frame.size.height -60, self.view.frame.size.width - 20, 20);
    // self.typeLable.backgroundColor = [UIColor blueColor];
    self.typeLable.font = [UIFont systemFontOfSize:14];
    self.typeLable.textColor = [UIColor whiteColor];
    [self.imageView addSubview:self.typeLable];
    
    self.priceLable = [[UILabel alloc] init];
    self.priceLable.frame = CGRectMake(10, self.imageView.frame.size.height -40, self.view.frame.size.width - 20, 20);
    //self.priceLable.backgroundColor = [UIColor greenColor];
    self.priceLable.font = [UIFont systemFontOfSize:14];
    self.priceLable.textColor = [UIColor whiteColor];
    [self.imageView addSubview:self.priceLable];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allModelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"twoCell" forIndexPath:indexPath];
    HerosChuZhuangModel *model = self.allModelArray[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    cell.titleLable.text = model.title;
    cell.nameLable.text = @"作者";
    cell.lable.text = model.author;
    cell.lable1.text = @"前期";
    cell.lable2.text = @"中期";
    cell.lable3.text = @"后期";
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *array1 = [model.pre_cz componentsSeparatedByString:@","];
    if (array1.count >= 1) {
        [cell.imv1_1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array1[0]]]];
    }
    if (array1.count >=2) {
        [cell.imv1_2 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array1[1]]]];
    }
    if (array1.count >=3) {
        [cell.imv1_3 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array1[2]]]];
    }
    if (array1.count >= 4) {
        [cell.imv1_4 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array1[3]]]];
    }if (array1.count >=5) {
        [cell.imv1_5 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array1[4]]]];
    }if (array1.count >= 6) {
        [cell.imv1_6 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array1[5]]]];
    }
    NSArray *array2 = [model.mid_cz componentsSeparatedByString:@","];
    if (array2.count >= 1) {
        [cell.imv2_1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array2[0]]]];
    }
    if (array2.count >=2) {
        [cell.imv2_2 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array2[1]]]];
    }
    if (array2.count >=3) {
        [cell.imv2_3 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array2[2]]]];
    }
    if (array2.count >= 4) {
        [cell.imv2_4 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array2[3]]]];
    }if (array2.count >=5) {
        [cell.imv2_5 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array2[4]]]];
    }if (array2.count >= 6) {
        [cell.imv2_6 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array2[5]]]];
    }
    
    NSArray *array3 = [model.end_cz componentsSeparatedByString:@","];
    if (array3.count >= 1) {
        [cell.imv3_1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array3[0]]]];
    }
    if (array3.count >=2) {
        [cell.imv3_2 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array3[1]]]];
    }
    if (array3.count >=3) {
        [cell.imv3_3 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array3[2]]]];
    }
    if (array3.count >= 4) {
        [cell.imv3_4 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array3[3]]]];
    }if (array3.count >=5) {
        [cell.imv3_5 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array3[4]]]];
    }if (array3.count >= 6) {
        [cell.imv3_6 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/zb/%@_64x64.png",array3[5]]]];
    }
    
    
    
    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.allModelArray.count - 1) {
        return 200;
    }
    return 160;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HeroNextCzNextTableViewController *hncntVc = [[HeroNextCzNextTableViewController alloc] init];
    HerosPifuModelHelper *helper = [HerosPifuModelHelper defaultHerosPifuHelper];
    if (helper.nameArray.count >=2) {
        hncntVc.name = helper.nameArray[1];
    }else{
        hncntVc.name = helper.nameArray[0];
    }
    
    if (helper.imageArray.count >=2) {
        hncntVc.imageUrl = helper.imageArray[1];
    }else{
        hncntVc.imageUrl = helper.imageArray[0];
    }
    
    NSArray *array1 = [helper.price componentsSeparatedByString:@","];
    hncntVc.price = [NSString stringWithFormat:@"金币:%@ 点卷:%@",array1[0],array1[1]];
    hncntVc.type = helper.type;
    
    hncntVc.model = self.allModelArray[indexPath.row];
    hncntVc.aString = self.aString;
    [self.navigationController pushViewController:hncntVc animated:YES];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat yOffset  = scrollView.contentOffset.y;
    if (yOffset < -kImageHeight) {
        CGRect f = self.imageView.frame;
        f.origin.y = yOffset;
        f.size.height =  -yOffset;
        self.imageView.frame = f;
    }
    self.nameLable.frame = CGRectMake(10, self.imageView.frame.size.height -80, self.view.frame.size.width-20, 20);
    self.typeLable.frame = CGRectMake(10, self.imageView.frame.size.height -60, self.view.frame.size.width - 20, 20);
    self.priceLable.frame = CGRectMake(10, self.imageView.frame.size.height -40, self.view.frame.size.width - 20, 20);
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
